package com.admin.service.libraryservice;

import com.admin.dao.librarydao.LibraryPageDaoInF;
import com.admin.dao.librarydao.LibraryPageImpl;
import com.admin.dto.BookTopListBean;
import com.admin.dto.UserSendBean;
import com.pojo.BorrowerBean;
import com.pojo.UserSendMsgBean;
import com.utils.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class LibraryPageServiceImpl implements LibraryPage{


    private LibraryPageDaoInF libraryDaoInF=null;

    public LibraryPageServiceImpl() {
        libraryDaoInF = new LibraryPageImpl();
    }

    @Override
    public List<UserSendBean> UserSendMsg() {
        List<UserSendBean> list = null;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "select b.username,u.msg,u.msgsendtime from usersendmsg u,borrower b where b.userid=u.boruserid";
            list = libraryDaoInF.getUserMsgInfo(sql, conn);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public String SenderuserName(Long userid){
        String username=null;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "select username from borrower where userid=?";
            username = libraryDaoInF.getUserNameInfo(sql, conn,userid);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return username;
    }

    @Override
    public List<BookTopListBean> BOOK_TOP_LIST_BEAN_LIST() {
        List<BookTopListBean> list = null;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "SELECT  books.bookname  ,(books.Originalstock-books.stock) sales  from books\n" +
                    "where (books.Originalstock-books.stock) and books.islower='0'\n" +
                    "ORDER BY sales DESC LIMIT 0,5";
            list = libraryDaoInF.BookTopInfo(sql, conn);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;

    }
}
